import { defineStore } from "pinia";
import { getDicData } from "@/api/modules/system";

export const useSelectStore = defineStore("select", {
  state: () => {
    return {
      dicData: {} as Record<string, any[]>
    };
  },
  actions: {
    setDic(key: string, list: any[]) {
      this.dicData[key] = list;
    },
    async getDic(key: string): Promise<any[]> {
      if (this.dicData[key] && this.dicData[key].length > 0) {
        return this.dicData[key];
      } else {
        const res = await getDicData(key);
        const data = res.data as unknown as any[];
        const list = data.map(item => ({
          value: item.value,
          label: item.description
        }));
        this.setDic(key, list);
        return list;
      }
    }
  }
});
